/**
 * @author Troy Gardner
 */
class com.troyworks.sos.SOS extends Object{

	private var _csock : XMLSocket;

	private var _msock : XMLSocket;
	/* WARNING NO TRACE STATEMENTS INSIDE OF THIS CLASS WHEN USING
	 * MTASC SOSLogger as you -trace out put!!
	 */
	public function SOS() {
		super();
	}
	public function getStatus():String{
		var res = "csock " + _csock + "  msock " + _msock;
		return res;
	}
	public function getCommandSocket() : XMLSocket{
		if(_csock == null){
			var 	csock : XMLSocket = new XMLSocket();
			csock.connect("localhost",4445);
		//WARNING: any one of these are needed for traces to show up
		// this for MTASC to keep it around
		//	_root.csock = csock;
		//	_global.csock = csock;
			_csock = csock;
		}
		return _csock;		
	}
	public function getMessageSocket() : XMLSocket{
		if(_msock == null){
			var 	msock : XMLSocket = new XMLSocket();
			msock.connect("localhost",4444);
		//WARNING: any one of these are needed for traces to show up
		// this for MTASC to keep it around
		//	_root.msock = msock;
		//	_global.csock = csock;
			_msock = msock;
		
		}
		return _msock;		
	}
	/////////////////////////////////////////////////////////////////
	public function createDialog(title : String, message : String) : Void{
		var 	csock : XMLSocket = getCommandSocket();
		csock.send("<commands>"+
			"<showError>"+
			"<title>"+title+"</title>"+
			"<message>"+message+"</message>"+
			"</showError>" +
 		     "</commands>\n");
	}
	public function showMessage(key : String, sMessage : Object) : Void{
		var 	csock : XMLSocket = getCommandSocket();
		//trace("showMessage " + key + " " + sMessage);
		if(typeof(sMessage)== "string" && String(sMessage).indexOf("\r") > -1){
				var _ary : Array = String(sMessage).split("\r");
				var res:String ="";//"<showMessage key='"+key + "'>"+sMessage+"</showMessage>\n";
				res+= "<showFoldMessage key='"+key+"'>";
				res+=  "<title>"+_ary[0]+"</title>";
			res+=  "<message>";
			//trace("nested message ");
				for(var i : Number = 1; i < _ary.length; i++){
					var s:String = "\t [" + i + "] = " + _ary[i] + "\n";
					//trace("adding string " +s);
					res+= s;		
				}
				res+="</message>";
				res+="</showFoldMessage>";
				res+= "\n";
						
				csock.send(res);
			}else{
				csock.send("<showMessage key='"+key + "'>"+sMessage+"</showMessage>\n");
			}
	}
	public function clearConsole() : Void {
		var csock:XMLSocket = getCommandSocket();
		csock.send("<commands>"+"<clear/>" + "</commands>\n");
	}

}